package com.huang.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.educenter.entity.UcenterMember;
import com.huang.educenter.entity.vo.LoginVo;
import com.huang.educenter.entity.vo.RegisterVo;
import com.huang.educenter.mapper.UcenterMemberMapper;
import com.huang.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.servicebase.execptionhandler.huangException;
import com.hunag.utils.JwtUtils;
import com.hunag.utils.MD5;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-29
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {

            throw new huangException(20001, "手机号不能为空");
        }
        UcenterMember one = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (one == null) {
            return "error";
            //  throw new huangException(20001,"手机号不存在");
        }
        //判断密码 数据库的密码是被加密的   先加密再比较
        password = MD5.encrypt(password);
        if (!password.equals(one.getPassword())) {
//            throw new huangException(20001,"账号或密码错误");
            return "error";
        }
        if (!one.getIsDisabled().equals(one.getIsDisabled())) {
            throw new huangException(20001, "登陆失败");
        }
        //登陆成功   Jwt生成token返回
        String token = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        return token;
    }

    @Override
    public void registerUser(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new huangException(20001, "注册失败");
        }
        String s = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(s)) {
            throw new huangException(20001, "注册失败");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new huangException(20001, "注册失败");
        }
        //验证完成  进行注册
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        baseMapper.insert(member);
    }

    @Override
    public Integer countRegisterDay(String day) {
        Integer count = baseMapper.countRegisterDay(day);
        return count;
    }
}
